/**
 * 
 */
package au.com.betaversion.service;

import java.util.Collection;
import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import au.com.betaversion.domain.RegisteredUser;
import au.com.betaversion.repository.RegisteredUserRepository;

/**
 * @author 103085
 *
 */
public class UserDetailsServiceImpl implements UserDetailsService {
	
	@Autowired
	private RegisteredUserRepository registeredUserRepository;

	/* (non-Javadoc)
	 * @see au.com.betaversion.service.UserDetailsService#loadUserByUsername(java.lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username) {
		RegisteredUser registeredUser = registeredUserRepository.findOne(username);
		
		Collection<SimpleGrantedAuthority> authorities = new HashSet<SimpleGrantedAuthority>();
		authorities.add(new SimpleGrantedAuthority(registeredUser.getRole()));
		
		User user = new User(registeredUser.getUsername(), registeredUser.getPassword(), authorities);
		
		return user;
	}

}
